import { getTopCategoryAPI } from '@/apis/category.js'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'

// 封装分类相关业务代码
export function useCategory() {
  const categoryData = ref({})
// 获取数据
const route = useRoute()
const getCategory = async (id = route.params.id) => {
  const res = await getTopCategoryAPI(id)
  categoryData.value = res.result
}
onMounted(() => getCategory())

// 路由参数发生变化时 可以把分类数据接口重新发送
onBeforeRouteUpdate((to)=>{
  getCategory(to.params.id)
})
// 把需要的数据return出去
return {
  categoryData
}
}